package com.wedimob.common;

import com.wedimob.bo.SerializableBO;


public class Size extends SerializableBO<Size> {

	private Long octets;

	public Size(final Long octets) {
		super();
		this.octets = octets;
	}

	public Long getOctets() {
		return octets;
	}

	public String getReadableSize() {
		return humanReadableByteCount(false);
	}

	public String getReadableSISize() {
		return humanReadableByteCount(true);
	}

	protected String humanReadableByteCount(boolean si) {
		int unit = si ? 1000 : 1024;
		if (octets < unit)
			return octets + " B";
		int exp = (int) (Math.log(octets) / Math.log(unit));
		String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1)
				+ (si ? "" : "i");
		return String.format("%.1f %sB", octets / Math.pow(unit, exp), pre);
	}
}
